/* coding with UTF-8 */
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
/* include core */
#include "main.h"
#include "tim.h"
#include "Gpio.h"
#include "Usart.h"
#include "Flash.h"
#include "Spi.h"
#include "Iwdg.h"
/* include BSP */
#include "w25qxx_Handler.h"
#include "AT24Cxx_Driver.h"
/* include Middlewares */
#include "ymodem.h"
#include "AES.h"
/* include Task */
#include "Debug.h"
#include "Boot_Manager.h"

/** @addtogroup Template_Project
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t uwTimingDelay;
RCC_ClocksTypeDef RCC_Clocks;


/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

///* 串口重定向 */
//#ifdef __GNUC__
//    #define PUTCHAR_PROTOTYPE int _io_putchar(int ch)
//#else
//    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
//#endif /* __GNUC__*/
// 
///******************************************************************
//    *@brief  Retargets the C library printf  function to the USART.
//    *@param  None
//    *@retval None
//******************************************************************/
//PUTCHAR_PROTOTYPE
//{
//    // HAL_UART_Transmit(&huart1, (uint8_t *)&ch,1,0xFFFF);
//        SEGGER_RTT_PutChar(0,ch);
//    return ch;
//}


/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	/* Enable Clock Security System(CSS): this will generate an NMI exception
     when HSE clock fails *****************************************************/
    RCC_ClockSecuritySystemCmd(ENABLE);
	
    /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files before to branch to application main.
       To reconfigure the default setting of SystemInit() function, 
       refer to system_stm32f4xx.c file */

    /* SysTick end of count event each 1ms */
    SystemCoreClockUpdate();
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);

    /* Init basic peripheral */
    USART1_Init();
    Key_IO_Init();
    Led_IO_Init();
    SPI1_Init();
	
    easy_logger_RTT_init();
    log_i("=== Here is bootloader! ===");
    uint8_t ret_W25Q64_Init = W25Q64_Init();
    if (0 != ret_W25Q64_Init) {
        log_e("error: W25Q64_Init() failed!");
    }
	else{
		log_i("W25Q64 init success!");
	}
    uint8_t ret_ee_check = ee_CheckOk();
    if (1 != ret_ee_check) {
        log_e("error: ee_CheckOk() failed!");
    }
	else{
		log_i("AT24C02 init success!");
	}
	// ee_Erase();

    OTA_StateManager();

    /* Add your application code here */
    
    /* Infinite loop */
    while (1)
    {
        /* 运行到这说明当前App程序文件已经损坏，无法跳转，需要下载新App固件 */
        Key_Scan_Download();
    }
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{ 
  uwTimingDelay = nTime;

  while(uwTimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (uwTimingDelay != 0x00)
  { 
    uwTimingDelay--;
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
